<?php


namespace app\index\controller;


use app\common\model\Article as ArticleModel;
use think\facade\View;

class Article extends Base
{

    public function read()
    {
        $id = $this->request->param('id/d');
        $articleInfo = ArticleModel::withCount(['comments'])->find($id);
        $articleInfo->inc('reading_count', 1)->update();
        $articleInfo['tag'] = explode(',', $articleInfo['tag']);
        $prevInfo = ArticleModel::where('id', '<', $id)->field('id,title')->find();
        $nextInfo = ArticleModel::where('id', '>', $id)->field('id,title')->find();
        $prev['title'] = $next['title'] = '没有更多内容了....';
        $prev['url'] = $next['url'] = 'javascript:;';
        if (!empty($prevInfo)) {
            $prev['title'] = $prevInfo['title'];
            $prev['url'] = url('article_read', ['id' => str_pad($prevInfo['id'], 15, '0', STR_PAD_LEFT)]);
        }
        if (!empty($nextInfo)) {
            $next['title'] = $nextInfo['title'];
            $next['url'] = url('article_read', ['id' => str_pad($nextInfo['id'], 15, '0', STR_PAD_LEFT)]);
        }

        $likeRankingData = ['点赞排行', 'javascript:;', 'like desc', 'cache_by_like', 'id,title,`like` as total'];
        $ranking = ArticleModel::getArticleRankingList(...$likeRankingData);
        //标签列表
        $tagList = ArticleModel::getTagList();
        //右侧导航内容
        $footerData['like'] = $articleInfo['like'];
        $footerData['dislike'] = $articleInfo['dislike'];
        $footerData['comment'] = $articleInfo['comments_count'] ?? 0;
        $footerData['id'] = $id;
        $footerData['type'] = 'article';
        $footerData['link'] = url('comment_like_or_dislike');


        View::assign(
            [
                'next'         => $next,
                'prev'         => $prev,
                'article_info' => $articleInfo,
                'ranking'      => $ranking,
                'tag_list'     => $tagList,
                'footer_data'  => $footerData,
            ]
        );
        //获取列表
        assign_config('comment_list', (string)url('comment_list'));
        assign_config('comment_publish', (string)url('comment_publish'));
        $this->assignBasic($articleInfo);
        return View::fetch();
    }
}
